package com.banshuibao.bsb_android.util;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;

import com.banshuibao.bsb_android.MyApplication;

import java.io.IOException;

/**
 * Created by wangpan on 2017/12/28.
 */

public class MediaPlayerUtil {
    private MediaPlayer mediaPlayer;
    private Context context;
    private MediaPlayerPlayingListener mediaPlayerPlayingListener;

    public MediaPlayerUtil(Context context) {
        this.context = context;
    }

    /**
     * 播放本地音频
     *
     * @param path
     * @return
     */
    public MediaPlayer getLocalMedia(String path) {
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setOnPreparedListener(onPreparedListener);
            mediaPlayer.setOnErrorListener(onErrorListener);
            mediaPlayer.setOnCompletionListener(onCompletionListener);
        }
        try {
          //  Uri uri = Uri.parse(path);
            mediaPlayer.reset();
            mediaPlayer.setDataSource(path);//指定音频文件路径
            mediaPlayer.prepare();//初始化播放器MediaPlayer

        } catch (IOException e) {
            e.printStackTrace();
        }

        return mediaPlayer;
    }

    /**
     * 播放网络音频
     */
    public MediaPlayer getNetMedia(String path) {
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setOnPreparedListener(onPreparedListener);
            mediaPlayer.setOnErrorListener(onErrorListener);
            mediaPlayer.setOnCompletionListener(onCompletionListener);
        }
        try {
            Uri uri = Uri.parse(path);
            mediaPlayer.reset();
            mediaPlayer.setDataSource(MyApplication.applicationInstance, uri);//指定音频文件路径
            mediaPlayer.prepareAsync();//初始化播放器MediaPlayer
        } catch (IOException e) {
            e.printStackTrace();
        }

        return mediaPlayer;
    }

    MediaPlayer.OnErrorListener onErrorListener = new MediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
            XToast.showToast(context, "播放异常");
            if (mediaPlayerPlayingListener != null) {
                mediaPlayerPlayingListener.OnError(mediaPlayer);
            }

            return false;
        }
    };

    MediaPlayer.OnCompletionListener onCompletionListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            XToast.showToast(context, "播放完成");
            setStop();
            if (mediaPlayerPlayingListener != null) {
                mediaPlayerPlayingListener.OnCompletion(mediaPlayer);
            }

        }
    };

    MediaPlayer.OnPreparedListener onPreparedListener = new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mediaPlayer) {
            mediaPlayer.seekTo(0);
            mediaPlayer.start();
            if (mediaPlayerPlayingListener != null) {
                mediaPlayerPlayingListener.OnPrepared(mediaPlayer);
            }
        }
    };


    public void setPause() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
        }
    }

    public void setPlay() {
        if (mediaPlayer != null && !mediaPlayer.isPlaying()) {
            mediaPlayer.start();
        }
    }

    public void setStop() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
            mediaPlayer.stop();
        }
    }

    public interface MediaPlayerPlayingListener {
        void OnError(MediaPlayer mediaPlayer);

        void OnPrepared(MediaPlayer mediaPlayer);

        void OnCompletion(MediaPlayer mediaPlayer);
    }

    public void setOnMediaPlayerPlayingListener(MediaPlayerPlayingListener listener) {
        this.mediaPlayerPlayingListener = listener;
    }

    public void clearMediaPlayer() {
        if (mediaPlayer != null) {
            mediaPlayer.pause();
            mediaPlayer.stop();
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer = null;
        }

    }
}
